package com.springboot.java_doc_searcher;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.springboot.java_doc_searcher.searcher.DocSearcher;
import com.springboot.java_doc_searcher.searcher.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description :
 * User: jiume
 * Date: 2025-06-10
 * Time: 14:23
 */
@RestController
public class Controller {

    private static final DocSearcher docSearcher = new DocSearcher();

    private static final ObjectMapper objectMapper = new ObjectMapper();

    // 需要将content-type设置为json，前端才能正常解析
    @GetMapping(value = "/search", produces = "application/json")
    public String search(String query) throws JsonProcessingException {
        // 调用DocSearch类的search方法
        List<Result> results = docSearcher.search(query);
        // 将查询结果格式化为JSON数据
        return objectMapper.writeValueAsString(results);
    }
}
